/**
 * Created by arvin on 2017/4/1 0001.
 */
/**
 * Created by arvin on 2017/3/31 0031.
 */
(function ($) {
    'use strict';

    // TOOLS DEFINITION
    // ======================

    var cachedWidth = null;

    // it only does '%s', and return '' when arguments are undefined
    var sprintf = function (str) {
        var args = arguments,
            flag = true,
            i = 1;

        str = str.replace(/%s/g, function () {
            var arg = args[i++];

            if (typeof arg === 'undefined') {
                flag = false;
                return '';
            }
            return arg;
        });
        return flag ? str : '';
    };

    // TreeGrid CLASS DEFINITION
    // ======================

    var TreeGrid = function (el, options) {
        this.options = options;
        this.$el = $(el);
        this.init();
    };

    TreeGrid.DEFAULTS = {
        classes:'tree-grid-table',
        fixedheader:true,
        fixedcolumns:true,
        formatLoadingMessage:function(){
            return 'Loading, please wait...';
        },

    };
    TreeGrid.COLUMN_DEFAULTS = {


    };
    TreeGrid.EVENTS = {
        'all.bs.tree': 'onAll'
    };

    TreeGrid.prototype.init = function () {
        console.log('init');
        this.initContainer();
        //this.initTable();
        //this.initHeader();
        //this.initData();
        //this.initFooter();
        //this.initToolbar();
        //this.initPagination();
        //this.initBody();
        //this.initSearchText();
        //this.initServer();
    };
    TreeGrid.prototype.initContainer = function () {
        this.$container = $([
            '<div class="tree-grid">',
            '<div class="tree-grid-toolbar"></div>',
            '<div class="tree-grid-container">',
            '<div class="tree-grid-header none"><table></table></div>',
            '<div class="tree-grid-fixed none"><table></table></div>',
            '<div class="tree-grid-body">',
            '<div class="tree-grid-loading">',
            this.options.formatLoadingMessage(),
            '</div>',
            '</div>',
            '</div>'
        ].join(''));
        this.$container.insertAfter(this.$el);
        this.$treeContainer = this.$container.find('.tree-grid-container');
        this.$treeToolbar = this.$container.find('.tree-grid-toolbar');
        this.$treeHeader = this.$container.find('.tree-grid-header');
        this.$treeBody = this.$container.find('.tree-grid-body');
        this.$treeLoading = this.$container.find('.tree-grid-loading');
        this.$treeBody.append(this.$el);
        this.$el.addClass(this.options.classes);
        if(this.options.fixedheader){
            this.$treeHeader.removeClass('none');
        }
        if(this.options.fixedcolumns){
            this.$treeHeader.removeClass('none');
        }
    };
    TreeGrid.prototype.trigger = function (name) {
        var args = Array.prototype.slice.call(arguments, 1);

        name += '.bs.tree';
        this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
        this.$el.trigger($.Event(name), args);

        this.options.onAll(name, args);
        this.$el.trigger($.Event('all.bs.tree'), [name, args]);
    };

    // TreeGrid PLUGIN DEFINITION
    // =======================

    var allowedMethods = [
        'getOptions',
        'getSelections', 'getAllSelections', 'getData',
        'refresh',
        'destroy',
        'showLoading', 'hideLoading',
        'refreshOptions',
        'resetSearch',
        'expandAll', 'collapseAll'

    ];

    $.fn.TreeGrid = function (option) {
        var value,
            args = Array.prototype.slice.call(arguments, 1);

        this.each(function () {
            var $this = $(this),
                data = $this.data('treegrid.tree'),
                options = $.extend({}, TreeGrid.DEFAULTS, $this.data(),
                    typeof option === 'object' && option);

            if (typeof option === 'string') {
                if ($.inArray(option, allowedMethods) < 0) {
                    throw new Error("Unknown method: " + option);
                }

                if (!data) {
                    return;
                }

                value = data[option].apply(data, args);

                if (option === 'destroy') {
                    $this.removeData('treegrid.tree');
                }
            }

            if (!data) {
                $this.data('treegrid.tree', (data = new TreeGrid(this, options)));
            }
        });

        return typeof value === 'undefined' ? this : value;
    };

    $.fn.TreeGrid.Constructor = TreeGrid;
    $.fn.TreeGrid.defaults = TreeGrid.DEFAULTS;
    $.fn.TreeGrid.columnDefaults = TreeGrid.COLUMN_DEFAULTS;
    $.fn.TreeGrid.methods = allowedMethods;
    $.fn.TreeGrid.utils = {
        sprintf: sprintf

    };

    // TreeGrid INIT
    // =======================

    $(function () {
        $('[data-toggle="tree"]').TreeGrid();
    });
})(jQuery);
